?Bug in tempStringOf?

There's an undocumented function, tempStringOf(Buffer), which supposedly works without adding another string to the string table (except during the call, of course). It otherwise should act identically to stringOf(Buffer). However, the following code produces unexpected results:

 

string s = ""
Buffer buf = create
buf = "ABC"
s = stringOf(buf)
print "s=" s "\n"
delete buf
print "s=" s "\n"

string s2 = ""
Buffer buf2 = create
buf2 = "DEF"
s2 = tempStringOf(buf2)
print "s2=" s2 "\n"
delete buf2
print "s2=" s2 "\n"

Output of the last print statement varies with the argument passed in, but for this string value I get:

s=ABC
s=ABC
s2=DEF
s2=F

It's as if the deletion of buf2 causes the first characters in s2 to be reinterpreted as Unicode, but printed in ASCII - although I haven't checked to see if those values line up.

Regardless, once s2 is assigned buf2 shouldn't have any effect on its value.

Is this a bug in this (admittedly undocumented) function, or am I missing something else going on?


JoeMarfice - Thu Apr 04 12:36:40 EDT 2019

Re: ?Bug in tempStringOf?
Mike.Scharnow - Thu Apr 04 12:50:58 EDT 2019

"s = stringOf(buf)" creates a copy of buf's string value into the string table and s points to this copy, so that the actual value is independent on whatever you do with buf afterwards.

"s2 = tempStringOf(buf2)" on the other hand makes s2 like a pointer directly inside the Buffer to the place where the value of the Buffer is stored. Thus, the value of s2 is only meaningful as long as the Buffer exists and as long as it is not changed. After "delete buf2", s2 must not be used any more, as the memory is (or may be) freed.

 

Re: ?Bug in tempStringOf?
JoeMarfice - Thu Apr 04 17:05:11 EDT 2019

Mike.Scharnow - Thu Apr 04 12:50:58 EDT 2019

"s = stringOf(buf)" creates a copy of buf's string value into the string table and s points to this copy, so that the actual value is independent on whatever you do with buf afterwards.

"s2 = tempStringOf(buf2)" on the other hand makes s2 like a pointer directly inside the Buffer to the place where the value of the Buffer is stored. Thus, the value of s2 is only meaningful as long as the Buffer exists and as long as it is not changed. After "delete buf2", s2 must not be used any more, as the memory is (or may be) freed.

 

Wow, thanks, that makes a lot of sense!

I suppose I should just use stringOf(Buffer) for situations where the buffer won't last until the end of the program (or when it has served it's purpose fully).

 

Re: ?Bug in tempStringOf?
Mike.Scharnow - Fri Apr 05 02:23:21 EDT 2019

JoeMarfice - Thu Apr 04 17:05:11 EDT 2019

Wow, thanks, that makes a lot of sense!

I suppose I should just use stringOf(Buffer) for situations where the buffer won't last until the end of the program (or when it has served it's purpose fully).

 

Yes, I only use it in situations where I e.g. print out the value to a file and do not need it anymore afterwards.

I guess this is one of the reasons why this perm is still undocumented, too error prone to create memory faults

Re: ?Bug in tempStringOf?
llandale - Mon Apr 08 16:06:12 EDT 2019

JoeMarfice - Thu Apr 04 17:05:11 EDT 2019

Wow, thanks, that makes a lot of sense!

I suppose I should just use stringOf(Buffer) for situations where the buffer won't last until the end of the program (or when it has served it's purpose fully).

 

Mike is correct.

Use tempStringOf only when you intend to dispose of the string value <<immediately>>.  "Dispose" includes printing, exporting, setting to an attribute value, setting into a dialog box, moving to another Buffer, or something else I'm not recalling.  If the function retrieving the value doesn't know for sure, then use "stringOf".

This demonstrates it well:
Buffer buf = create()

buf = "ABCD"

string Result = tempStringOf(buf)

print "Before " Result "\n"

buf = "DEF"

print "After " Result "\n"  // This prints DEF, even though Result wasn't modified.

 

-Louie